using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class LeaderboardsAdapter
{
	public class SingleLeaderboardAdapter
	{
		protected List<ClientPrize> emptyPrizes = new List<ClientPrize>();

		public virtual List<ClientPrize> prizes
		{
			get
			{
				return emptyPrizes;
			}
		}

		public virtual bool isPlayerRanked
		{
			get
			{
				return player != null && player.leaderboardIndex >= 0;
			}
		}

		public virtual string name
		{
			get
			{
				return string.Empty;
			}
		}

		public virtual bool isEmpty
		{
			get
			{
				return true;
			}
		}

		public virtual Lead.Leaderboard.Ranking player
		{
			get
			{
				return null;
			}
		}

		public virtual ClientPrize winnings
		{
			get
			{
				return null;
			}
		}
	}

	public class SingleNonSegmentedLeaderboard : SingleLeaderboardAdapter
	{
		public Lead.Leaderboard leaderboard;

		private List<ClientPrize> clientPrizes = new List<ClientPrize>();

		public override string name
		{
			get
			{
				return leaderboard.name;
			}
		}

		public override bool isEmpty
		{
			get
			{
				return leaderboard.rankings.Count == 0;
			}
		}

		public override Lead.Leaderboard.Ranking player
		{
			get
			{
				return leaderboard.player;
			}
		}

		public override List<ClientPrize> prizes
		{
			get
			{
				if (leaderboard.prizes != null)
				{
					if (clientPrizes.Count != leaderboard.prizes.Count)
					{
						clientPrizes.Clear();
						foreach (Lead.Leaderboard.Prize prize in leaderboard.prizes)
						{
							clientPrizes.Add(new ClientPrize(prize));
						}
					}
					return clientPrizes;
				}
				return emptyPrizes;
			}
		}

		public override ClientPrize winnings
		{
			get
			{
				return new ClientPrize(leaderboard.winnings);
			}
		}

		public SingleNonSegmentedLeaderboard(Lead.Leaderboard leaderboard)
		{
			this.leaderboard = leaderboard;
		}
	}

	public class SingleSegmentedLeaderboard : SingleLeaderboardAdapter
	{
		public Lead.SegmentedLeaderboard leaderboard;

		public override string name
		{
			get
			{
				return leaderboard.name;
			}
		}

		public override bool isEmpty
		{
			get
			{
				foreach (Lead.SegmentedLeaderboard.RankRange item in leaderboard.rankRange)
				{
					if (item.players.Count > 0)
					{
						return false;
					}
				}
				return true;
			}
		}

		public override Lead.Leaderboard.Ranking player
		{
			get
			{
				return leaderboard.player;
			}
		}

		public override ClientPrize winnings
		{
			get
			{
				return new ClientPrize(leaderboard.winnings);
			}
		}

		public SingleSegmentedLeaderboard(Lead.SegmentedLeaderboard leaderboard)
		{
			this.leaderboard = leaderboard;
		}
	}

	public class ClientPrize
	{
		private Lead.Leaderboard.Prize prize;

		public int index
		{
			get
			{
				if (prize == null)
				{
					return 0;
				}
				return prize.prize_rank;
			}
		}

		public string name
		{
			get
			{
				if (prize == null)
				{
					return string.Empty;
				}
				return prize.name;
			}
		}

		public int coins { get; protected set; }

		public int tokens { get; protected set; }

		public int cash { get; protected set; }

		public ClientPrize(Lead.Leaderboard.Prize prize)
		{
			this.prize = prize;
			coins = ((prize != null) ? (prize.prize_coins + GetInt("coins", prize.rewards)) : 0);
			cash = ((prize != null) ? (prize.prize_cash + GetInt("ggdollars", prize.rewards)) : 0);
			tokens = ((prize != null) ? GetInt("tokens", prize.rewards) : 0);
		}

		private int GetInt(string varName, string from)
		{
			string[] array = from.Split(',');
			string[] array2 = array;
			foreach (string text in array2)
			{
				string[] array3 = text.Split('=');
				if (array3.Length == 2 && array3[0] == varName)
				{
					return int.Parse(array3[1]);
				}
			}
			return 0;
		}
	}

	public List<SingleLeaderboardAdapter> leaderboards = new List<SingleLeaderboardAdapter>();

	public int competitionIndex { get; protected set; }

	public long compEndTimestamp { get; protected set; }

	public LeaderboardsAdapter(CombinationLeads lead)
	{
		if (lead.segmentedLeaderboards != null)
		{
			foreach (Lead.SegmentedLeaderboard segmentedLeaderboard in lead.segmentedLeaderboards)
			{
				leaderboards.Add(new SingleSegmentedLeaderboard(segmentedLeaderboard));
			}
		}
		if (lead.nonSegmentedLeaderboards != null)
		{
			foreach (Lead.Leaderboard nonSegmentedLeaderboard in lead.nonSegmentedLeaderboards)
			{
				leaderboards.Add(new SingleNonSegmentedLeaderboard(nonSegmentedLeaderboard));
			}
		}
		competitionIndex = lead.competitionIndex;
		compEndTimestamp = lead.compEndTimestamp;
	}

	public LeaderboardsAdapter(Lead lead)
	{
		if (lead.leaderboards != null)
		{
			foreach (Lead.Leaderboard leaderboard in lead.leaderboards)
			{
				leaderboards.Add(new SingleNonSegmentedLeaderboard(leaderboard));
			}
		}
		competitionIndex = lead.competitionIndex;
		compEndTimestamp = lead.compEndTimestamp;
	}

	public static Lead.Leaderboard.Ranking CreateRanking(int index, int eloScore = 1000, int score = 10000)
	{
		Lead.Leaderboard.Ranking ranking = new Lead.Leaderboard.Ranking();
		ranking.country = "ireland";
		ranking.leaderboardIndex = index;
		ranking.score = score.ToString();
		ranking.name = "Test User";
		ranking.stars = eloScore.ToString();
		ranking.playerId = Guid.NewGuid().ToString();
		return ranking;
	}

	public static Lead.SegmentedLeaderboard.RankRange CreateRankRange(string name, int users, int totalUsers)
	{
		Lead.SegmentedLeaderboard.RankRange rankRange = new Lead.SegmentedLeaderboard.RankRange();
		rankRange.prizes = new Lead.Leaderboard.Prize();
		for (int i = 0; i < users; i++)
		{
			rankRange.players.Add(CreateRanking(totalUsers + i, UnityEngine.Random.Range(400, 2800), 10000 - (totalUsers + i) * 10));
		}
		rankRange.name = name;
		return rankRange;
	}

	public static LeaderboardsAdapter CreateTestSegmentedLeaderboard()
	{
		CombinationLeads combinationLeads = new CombinationLeads();
		combinationLeads.compEndTimestamp = new TimeSpan(7, 0, 0, 0).Seconds;
		combinationLeads.competitionIndex = 0;
		Lead.SegmentedLeaderboard segmentedLeaderboard = new Lead.SegmentedLeaderboard();
		segmentedLeaderboard.rankRange.Add(CreateRankRange("First Place", 1, 0));
		segmentedLeaderboard.rankRange.Add(CreateRankRange("Second Place", 1, 1));
		segmentedLeaderboard.rankRange.Add(CreateRankRange("Third Place", 1, 2));
		Lead.SegmentedLeaderboard.RankRange rankRange = CreateRankRange("Top 5%", 10, 3);
		segmentedLeaderboard.rankRange.Add(rankRange);
		segmentedLeaderboard.rankRange.Add(CreateRankRange("Top 10%", 10, 13));
		segmentedLeaderboard.player = rankRange.players[3];
		segmentedLeaderboard.winnings = new Lead.Leaderboard.Prize();
		segmentedLeaderboard.winnings.name = "Top 5%";
		segmentedLeaderboard.winnings.prize_cash = 10;
		segmentedLeaderboard.winnings.prize_coins = 10;
		combinationLeads.segmentedLeaderboards.Add(segmentedLeaderboard);
		segmentedLeaderboard.name = "World";
		return new LeaderboardsAdapter(combinationLeads);
	}
}
